package com.situ.student.common;

import com.mysql.cj.jdbc.Driver;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;

public class Global {
    public static String driver;
    public static String jdbcUrl;
    public static String username;
    public static String password;

    //获取数据源，数据库连接池
    private static DataSource dataSource = null;

    //获取数据源，数据库连接池
    public static DataSource getDataSource() {
        if (dataSource == null) {
            try {
                Driver d = (Driver) BeanUtils.instantiateClass(Class.forName(driver));
                dataSource = new SimpleDriverDataSource(d, Global.jdbcUrl, Global.username, Global.password);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return dataSource;
    }


    //spring提供的jdbc模板操作类
    public static JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(getDataSource());
    }
}
